#ifndef __MYSQL_MANAGEMENT_H__
#define __MYSQL_MANAGEMENT_H__
#include <vector>
#include <thread>
#include <string>
#include <boost/asio.hpp>
#include "mysql_client.h"

class mysql_management {
public:
  mysql_management();

  void init(int thread_cnt,
            const std::string host,
            const std::string user,
            const std::string password,
            const std::string database,
            unsigned short port = 3306);

  // 线程执行主程序
  void run();

  // 停止mysql_management
  void stop();

  // select
  // arg1: sql语句
  // arg2: 要回调的io_context
  // arg3: 要回调的函数
  void
  select(std::string sql,
         boost::asio::io_context& io_context,
         std::function<void(bool is_success, std::vector<std::vector<std::string>> results)> func);

  // execute
  // arg1: sqls 要执行的sql语句（如果是多条语句，默认开启事物）
  // arg2: 要回调的io_context
  // arg3: 要回调的函数
  void execute(std::vector<std::string> sqls,
               boost::asio::io_context& io_context,
               std::function<void(bool is_success)> func);

private:
  std::string host_;
  std::string user_;
  std::string password_;
  std::string database_;
  unsigned short port_ = 3306;

  static thread_local mysql_client client_;
  boost::asio::io_context io_context_;
  std::vector<std::thread> threads_;

  typedef boost::asio::io_context::executor_type ExecutorType;
  boost::asio::executor_work_guard<ExecutorType> work_guard_;
};

#endif